package com.foreknow.elm.controller;

import com.foreknow.elm.po.DeliveryAddress;
import com.foreknow.elm.service.DeliveryAddressService;
import com.foreknow.elm.service.impl.DeliveryAddressServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class DeliveryAddressController {
    private int a;
    /**
     * 根据用户编号查询所属配送地址
     * @return
     */
    public Object listDeliveryAddressByUserId(HttpServletRequest request) throws Exception{
        //获取请求参数 DeliveryAddressController/listDeliveryAddressByUserId?userId=12345671111
        String userId = request.getParameter("userId");
        DeliveryAddressService deliveryAddressService = new DeliveryAddressServiceImpl();
        // 调用service方法
        List<DeliveryAddress> list = deliveryAddressService.listDeliveryAddressByUserId(userId);
        // 返回list(deliveryAddress)数组
        return  list;
    }

    /**
     * 根据配送地址编号查询送货地址
     * @return
     */
    public Object getDeliveryAddressById(HttpServletRequest request) throws Exception{
        //获取请求参数 DeliveryAddressController/getDeliveryAddressById?daId=4
        Integer daId = Integer.parseInt(request.getParameter("daId"));
        DeliveryAddressService service = new DeliveryAddressServiceImpl();
        // 调用service方法
        DeliveryAddress deliveryAddress = service.getDeliveryAddressById(daId);
        // 返回list(deliveryAddress)对象
        return deliveryAddress;
    }

    /**
     * 向送货地址表中添加一条记录
     * @return
     */
    public Object saveDeliveryAddress(HttpServletRequest request) throws Exception{
        // 获取请求参数 DeliveryAddressController/saveDeliveryAddress?contactName=xjx&contactSex=1&contactTel=12345674444&address=老鼠洞&userId=12345674444
        //联系人姓名
         String contactName = request.getParameter("contactName");
        //联系人性别
         Integer contactSex = Integer.parseInt(request.getParameter("contactSex"));
        //联系人电话
         String contactTel = request.getParameter("contactTel");
        //地址
         String address = request.getParameter("address");
        //所属用户编号
         String userId = request.getParameter("userId");
        DeliveryAddress deliveryAddress = new DeliveryAddress();
        deliveryAddress.setContactName(contactName);
        deliveryAddress.setContactSex(contactSex);
        deliveryAddress.setContactTel(contactTel);
        deliveryAddress.setAddress(address);
        deliveryAddress.setUserId(userId);
        DeliveryAddressService deliveryAddressService = new DeliveryAddressServiceImpl();
        // 调用service方法
        a = deliveryAddressService.saveDeliveryAddress(deliveryAddress);
        // 返回增加地址所影响的行数
        return a;
    }

    /**
     * 根据送货地址编号更新送货地址信息
     * @return
     */
    public Object updateDeliveryAddress(HttpServletRequest request) throws Exception{
        // 获取请求参数 DeliveryAddressController/updateDeliveryAddress?contactName=小姜旭&contactSex=1&contactTel=76543213333&address=老鼠洞&userId=12345673333&daId=4
        //联系人姓名
        String contactName = request.getParameter("contactName");
        //联系人性别
        Integer contactSex = Integer.parseInt(request.getParameter("contactSex"));
        //联系人电话
        String contactTel = request.getParameter("contactTel");
        //地址
        String address = request.getParameter("address");
        //所属用户编号
        String userId = request.getParameter("userId");
        //送货地址编号
        Integer daId = Integer.parseInt(request.getParameter("daId"));
        DeliveryAddress deliveryAddress = new DeliveryAddress();
        deliveryAddress.setContactName(contactName);
        deliveryAddress.setContactSex(contactSex);
        deliveryAddress.setContactTel(contactTel);
        deliveryAddress.setAddress(address);
        deliveryAddress.setUserId(userId);
        deliveryAddress.setDaId(daId);
        DeliveryAddressService deliveryAddressService = new DeliveryAddressServiceImpl();
        // 调用service方法
        a = deliveryAddressService.updateDeliveryAddress(deliveryAddress);
        // 返回修改地址所影响的行数
        return a;
    }

    /**
     * 根据送货地址编号删除一条记录
     * @return
     */
    public Object removeDeliveryAddress(HttpServletRequest request) throws Exception{
        // 获取请求参数 DeliveryAddressController/removeDeliveryAddress?daId=4
        Integer daId = Integer.parseInt(request.getParameter("daId"));
        DeliveryAddressService deliveryAddressService = new DeliveryAddressServiceImpl();
        // 调用service方法
        a = deliveryAddressService.removeDeliveryAddress(daId);
        // 返回删除地址所影响的行数
        return a;
    }
}
